using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DuplicateFileFinder
{
    public partial class frmSearchPaths : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmSearchPaths()
        {
            InitializeComponent();
        }

        private Engine mEngine = null;
        private bool mInUpdate = false;

        public void InitViewer(Engine pEngine)
        {
            mEngine = pEngine;
            UpdateViewer();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            //Environment.

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                SearchPath tSearchPath = new SearchPath(folderBrowserDialog1.SelectedPath);
                if (mEngine.SearchPathList.Exists(t => t.Path == tSearchPath.Path))
                {
                    MessageBox.Show(tSearchPath.Path + " already exists.");
                }
                else
                {
                    mEngine.SearchPathList.Add(tSearchPath);
                    UpdateViewer();
                }
            }

        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem tItem in lstView.SelectedItems)
            {
                SearchPath tSearchPath = ((SearchPath)tItem.Tag);
                mEngine.SearchPathList.RemoveAll(t => t.Path == tSearchPath.Path);

            }

            UpdateViewer();
        }

        public void UpdateViewer()
        {
            mInUpdate = true;
            if (InvokeRequired)
            {
                this.Invoke(new EventManager.DelegateUpdateViewer(UpdateViewer));
            }
            else
            {
                lstView.Items.Clear();

                foreach (SearchPath tSearchPath in mEngine.SearchPathList)
                {
                    AddListViewItem(tSearchPath);
                }
                lstView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
            mInUpdate = false;
        }

        private void AddListViewItem(SearchPath pSearchPath)
        {
            ListViewItem tItem = lstView.Items.Add(pSearchPath.Path);
            tItem.ToolTipText = pSearchPath.Path;
            tItem.Checked = pSearchPath.Selected;
            tItem.Tag = pSearchPath;
            tItem.SubItems.Add(pSearchPath.Recursive ? "Yes" : "No");
            tItem.SubItems.Add(pSearchPath.Pattern);
        }

        private void lstView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (mInUpdate) { return; }
            if (lstView.SelectedItems.Count > 0)
            {
                mInUpdate = true;
                SearchPath tSearchPath = ((SearchPath)lstView.SelectedItems[0].Tag);



                chkRecursive.Checked = tSearchPath.Recursive;



                txtPattern.Text = tSearchPath.Pattern;
                mInUpdate = false;
            }
        }

        private void chkRecursive_CheckedChanged(object sender, EventArgs e)
        {
            if (mInUpdate) { return; }
            foreach (ListViewItem tItem in lstView.SelectedItems)
            {

                SearchPath tSearchPath = ((SearchPath)tItem.Tag);

                tSearchPath.Recursive = chkRecursive.Checked;
                tItem.SubItems[1].Text = tSearchPath.Recursive ? "Yes" : "No";
            }
        }

        private void txtPattern_KeyUp(object sender, KeyEventArgs e)
        {
            if (mInUpdate) { return; }
            //mInUpdate = true;

            foreach (ListViewItem tItem in lstView.SelectedItems)
            {
                SearchPath tSearchPath = ((SearchPath)tItem.Tag);
                tSearchPath.Pattern = txtPattern.Text;
                tItem.SubItems[2].Text = tSearchPath.Pattern;

            }
            //mInUpdate = false;
        }

    }
}